import sys
from PySide2.QtGui import *
from PySide2.QtCore import *
from PySide2.QtWidgets import *

class FindWindow(QWidget):

    def __init__(self):
        super(FindWindow, self).__init__()
        self.radio_list=[]
        self.setWindowFlags(Qt.WindowStaysOnTopHint)
        self.setWindowTitle('Find Start')
        self.main_layout=QVBoxLayout()
        self.radio_layout=QVBoxLayout()
        self.init_radio()
        self.attr_layout=QHBoxLayout()
        self.attr_label=QLabel('Attribute:')
        self.attr_edit=QLineEdit()
        self.seach_btn=QPushButton('Goto')
        self.seach_btn.clicked.connect(self.goto)
        self.attr_layout.addWidget(self.attr_label)
        self.attr_layout.addWidget(self.attr_edit)
        self.attr_layout.addWidget(self.seach_btn)

        self.main_layout.addLayout(self.radio_layout)
        self.main_layout.addLayout(self.attr_layout)
        self.setLayout(self.main_layout)



    def init_radio(self):
        names=['Point','Prim','Vertice','Detail']
        for i in names:
            radio=QRadioButton(i)
            # radio.isChecked()
            self.radio_list.append(radio)
            self.radio_layout.addWidget(radio)
            if i=='Point':
                radio.setChecked(Qt.Checked)

    def goto(self):
        cls=''
        for i in self.radio_list:
            if i.isChecked():
                cls=i.text()
        attr=self.attr_edit.text()
        self.find(cls,attr)

    def find(self,cls,attr):
        sn = hou.selectedNodes()[0]
        cs = sn.cookPathNodes()
        find_nodes = []

        for c in cs:
            geo = c.geometry()
            fun = None
            if cls == 'Point':
                fun = geo.findPointAttrib
            elif cls == "Prim":
                fun = geo.findPrimAttrib
            elif cls == 'Vertice':
                fun = geo.findVertexAttrib
            elif cls == 'Detail':
                fun = geo.findGlobalAttrib
            f = fun(attr)
            if f:
                find_nodes.append(c)
        if find_nodes:
            start = find_nodes[-1]
            #print(start.path())
            start.setCurrent(1, 0)
        else:
            hou.ui.displayMessage('没有找到带该属性的节点')

f=FindWindow()
f.show()

